package com.lmz.project.redis.demo;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

import java.util.List;

public class LettuceDemo {
    public static void main(String[] args) {
        //1. 使用构建器，链式编程来builder我们的redisUri
        RedisURI  uri = RedisURI.builder()
                .redis("192.168.182.48")
                .withPort(6381)
                .withAuthentication("default","123456")
                .build();
        //2. 创建连接客户端
        RedisClient redisClient = RedisClient.create(uri);
        StatefulRedisConnection connect = redisClient.connect();

        //3. 通过conn创建操作的command sync同步的
        RedisCommands commands = connect.sync();

        //key
        List keys = commands.keys("*");
        System.out.println(keys);

        commands.set("key5","helloLettuce");
        System.out.println(commands.get("key5"));

        //list
        commands.lpush("list11","1","2","444");
        List<String> list11 = commands.lrange("list11", 0, -1);
        for (String s : list11) {
            System.out.println(s);
        }

        //hash
        commands.hset("hash","k1","v1");
        commands.hset("hash","k2","v2");
        commands.hset("hash","k3","v3");
        commands.hset("hash","k4","v4");
        System.out.println(commands.hgetall("hash"));
        Boolean hexists = commands.hexists("hash", "v2");
        System.out.println(hexists);

        //set
        commands.sadd("s1","1","2");
        System.out.println(commands.smembers("s1"));

        System.out.println(commands.sismember("s1","1")); //检验1是否再set中
        System.out.println(commands.scard("s1"));  //s1 集合的元素数量

        //zset
        commands.zadd("a1",100,"v1");
        commands.zadd("a1",80,"v2");
        System.out.println(commands.zrange("a1",0,-1));
        System.out.println(commands.zcount("a1","90","100")); //计算a1 集合中分数在90-100 之间的元素个数和

        //4.各种关闭释放资源
        connect.close();
        redisClient.shutdown();
    }
}
